<template>
  <div class="container">
    <div id="pieChart" style="height:100%" />
  </div>
</template>

<script>
const echarts = require('echarts/lib/echarts')
require('echarts/lib/component/title')
require('echarts/lib/component/tooltip')
require('echarts/lib/component/legend')
require('echarts/lib/chart/pie')

export default {
  data () {
    return {
      pieChart: null
    }
  },

  mounted () {
    this.init()
    this.getPieChart()
  },

  methods: {
    init () {
      this.pieChart = echarts.init(document.getElementById('pieChart'))
      window.addEventListener('resize', () => {
        this.pieChart.resize()
      })
    },

    getPieChart () {
      const option = {
        title: {
          text: '各种水果销售额的饼状图',
          subtext: '单位：元',
          left: 'center'
        },
        tooltip: {
          trigger: 'item',
          formatter: '{b} 销售额: {c} ({d}%)'
        },
        legend: {
          orient: 'vertical',
          left: 'left'
        },
        series: [
          {
            name: '销售额',
            type: 'pie',
            radius: 120,
            data: [
              { value: 100, name: '苹果' },
              { value: 200, name: '梨' },
              { value: 200, name: '桃' },
              { value: 300, name: '橘子' },
              { value: 200, name: '西瓜' }
            ],
            label: {
              formatter: '{b} 销售额: {c} ({d}%)'
            }
          }
        ]
      }
      this.pieChart.setOption(option)
    }
  }
}
</script>

<style lang="less" scoped>
.container {
  height: 100%;
  background-color: white;
  overflow-y: auto;
}
</style>
